package com.mingxun.project.http;

import com.mingxun.project.BuildConfig;
import com.mingxun.project.base.Constant;
import com.mingxun.project.service.ServiceApi;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * class name：Retrofit
 *
 * @author liuhongyuan.
 * @date 2017/12/19
 */

public class RetrofitHelper {
    public static RetrofitHelper httpClientApi;
    private ServiceApi serviceApi;

    public RetrofitHelper(){
        serviceApi = initHttpClientApi(ServiceApi.class);
    }

    public static RetrofitHelper getInstance(){
        if(httpClientApi == null){
            httpClientApi = new RetrofitHelper();
        }
        return httpClientApi;
    }

    private static <T> T initHttpClientApi(Class<T> clazz) {

        OkHttpClient client = HttpClient.newInstance().getHttpClient();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.DEBUG? Constant.API_BASE_TEST_URL: Constant.API_BASE_URL)
                .addConverterFactory(FastJsonConvertFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(client)
                .build();

        return retrofit.create(clazz);
    }

    public ServiceApi getServiceApi() {
        return serviceApi;
    }

    public void setServiceApi(ServiceApi serviceApi) {
        this.serviceApi = serviceApi;
    }
}
